#!/usr/bin/python
# -*- coding: utf-8 -*- 
import requests
import re
from hashlib import md5
import json
import sys
import os
import pymysql
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import calendar,datetime
conn = pymysql.connect(
        host = '10.37.133.21',
        port = 3306,
        user = 'spider',
        passwd = 'spider',
        db = 'spider',
        charset = 'utf8'
    )
cur = conn.cursor()
#利用无头浏览器获取cookie
def getcookie():
    chrome_options = Options()
    chrome_options.add_argument('headless')
    chrome_options.add_argument('disable-gpu')
    chrome_options.add_argument('no-sandbox')
    chrome_options.add_argument('disable-dev-shm-usage')
    # 在启动浏览器时加入配置
    #browser = webdriver.Chrome('/usr/bin/chromedriver', options=chrome_options)
    browser = webdriver.Chrome(options=chrome_options)
    browser.get('http://gw00257433:123.com@10.255.19.126:81/login.aspx/')
    # 等待加载，最多等待20秒
    browser.implicitly_wait(10)
    # 获取cookie
    cookjar = browser.get_cookies()
    cookie_str = ""
    # 组装cookie字符串
    for item_cookie in cookjar:
        item_str = item_cookie["name"] + "=" + item_cookie["value"] + "; "
        cookie_str += item_str
    print(cookie_str)
    return cookie_str
    browser.quit()
    #     print "当前页面内容------" + res.content
    # print "res2-------" + res2.text
    # with open("res.txt","a") as f:
    #     f.write(res.text.encode("utf-8"))
    #cookie = requests.utils.dict_from_cookiejar(cookie_str)
header = {

        'Connection': 'keep-alive',
        'X-Requested-With': 'XMLHttpRequest',
        'Cache-Control': 'max-age=0',
        'Upgrade-Insecure-Requests': '1',
        'Origin': 'http://itsm',
        'Content-Type': 'application/json;charset=UTF-8',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Referer': 'http://itsm/rest/urlRestFul/getRequestMannageUrl',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'cookie': getcookie()

    }
####获取当日，当月第一天，当月最后一天时间变量
today = datetime.date.today()
now = datetime.datetime.now()
year = now.year
month = now.month
last_day = calendar.monthrange(year,month)[1]## 最后一天
monstart = datetime.date(year,month,1)
monend = datetime.date(year,month,last_day)
print(now.strftime('%Y-%m-%d %H:%M:%S'))
# print (today)
# print(monstart)
# print(monend)
##日照园区今日工单
body1 = {
    "begintime": str(today),
    "endtime": str(today),
    "areaids": [
        "233","234","235","236","237","238","239","240","241","233","234","235","236","237","238","239","240","241","242","242","243","243"
    ]
}
##日照园区今日已解决工单
body2 = {
    "begintime": str(today),
    "endtime": str(today),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    "status":"4"
}
##日照园区今日已评价工单，已评价+已解决=实际已解决
body10 = {
    "begintime": str(today),
    "endtime": str(today),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    "status":"5"
}

##日照园区今日已挂起工单，已评价+已解决+已挂起=实际已解决
body12 = {
    "begintime": str(today),
    "endtime": str(today),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    "status":"7"
}

##日照园区当月低评工单
body3 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    "score":"11"

}
##日照园区当月满意度
body4 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    # "acceptcontent": ""
}
##日照园区当月评价率
body5 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    # "acceptcontent": "100005702"
}
##日照园区SLA及时解决率率,以综合办公楼为例
body6 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ]
}
##当月工单总数量
body7 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ]
}
##当月已解决工单
body8 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    "status":"4"
}
##当月已评价工单
body9 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    "status":"5"
}
##日照园区当月已挂起工单，已评价+已解决+已挂起=实际已解决
body11 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    "status":"7"
}
##日照本地组超时请求head
body13 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    "acceptgroupid": "1614",
    "sla": "3"

}
##日照远程组超时请求head
body14 = {
    "begintime": str(monstart),
    "endtime": str(monend),
    "areaids": [
        "233", "234", "235", "236", "237", "238", "239", "240", "241", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "242", "243", "243"
    ],
    "acceptgroupid": "1761",
    "sla": "3"

}
if __name__ == '__main__':

    s = requests.session()
    ####派单列表url
    url1 = 'http://itsm.corp.gwm.com/rest/requestRestFul/getPageList/0/20'
    ####客户满意度url
    url2 = 'http://itsm/rest/requestReportRestFul/getCountBySatisfaction/1/20'
    ####客户评价率
    url3 = 'http://itsm/rest/requestReportRestFul/getCountByEvaluate/1/20'
    ####SLA及时解决率url，数据不准确，重写及时解决率业务逻辑
    url4 = 'http://itsm/rest/requestReportRestFul/getCountBySolve/1/20'
    res = s.post(url1, headers=header, data=json.dumps(body1))
    str = res.text
    num1 = json.loads(str)['page']['resultCount']
    print(num1)
    res = s.post(url1, headers=header, data=json.dumps(body2))
    str = res.text
    num2 = json.loads(str)['page']['resultCount']
    print(num2)
    res = s.post(url1, headers=header, data=json.dumps(body10))
    str = res.text
    if len(json.loads(str)['page']['rows']) == 0:
        num10 = '0'
    else:
        num10 = int(json.loads(str)['page']['resultCount'])
    temp = int(num10) + int(num2)
    dayresolve = temp.__str__()
    print(dayresolve)
    res = s.post(url1, headers=header, data=json.dumps(body3))
    str = res.text
    num3 = json.loads(str)['page']['resultCount']
    print(num3)
    res = s.post(url2, headers=header, data=json.dumps(body4))
    str = res.text
    if len(json.loads(str)['page']['rows']) == 0:
        num4 = '0'
    else:
        a = len(json.loads(str)['page']['rows'])
        print(a)
        temp = 0.0
        i = 0
        for i in range(0, a):
            num4 = json.loads(str)['page']['rows'][i]['satisfaction']
            temp = float(num4) + temp
        temp = round((temp / a), 2)
        num4 = temp.__str__()
    print(num4)
    res = s.post(url3, headers=header, data=json.dumps(body5))
    str = res.text
    if len(json.loads(str)['page']['rows']) == 0:
        num5 = '0.0'
    else:
        a = len(json.loads(str)['page']['rows'])
        print(a)
        # temp = 0.0
        temp1 = 0
        temp2 = 0
        i = 0
        for i in range(0, a):
            # num5 = json.loads(str)['page']['rows'][i]['evaluaterate']
            # temp = float(num5) + temp
            temp1 = int(json.loads(str)['page']['rows'][i]['goodcount']) + temp1
            temp2 = int(json.loads(str)['page']['rows'][i]['solvedcount']) + temp2
        # temp = round((temp / a), 2)
        # num5 = temp.__str__()
        num5 = (round(((float(temp1) / float(temp2)) * 100), 2)).__str__()
    print('num5---------'+num5)
    res = s.post(url4, headers=header, data=json.dumps(body6))
    str = res.text
    if len(json.loads(str)['page']['rows']) == 0:
        num6 = '0'
    else:
        a = len(json.loads(str)['page']['rows'])
        print(a)
        temp = 0
        i = 0
        for i in range(0, a):
            num6 = int(json.loads(str)['page']['rows'][i]['solvedcount'])
            temp = num6 + temp
            temp = float(temp)
        ##temp为日照园区纳入sla考核的整理数量，res1为本地组sla超时数量，res2为远程组sla超时数量
        res1 = s.post(url1, headers=header, data=json.dumps(body13))
        res2 = s.post(url1, headers=header, data=json.dumps(body14))
        localSla = float(json.loads(res1.text)['page']['resultCount'])
        remoteSla = float(json.loads(res2.text)['page']['resultCount'])
        correctSla = temp - localSla - remoteSla
        num6 = (round((correctSla / temp), 4)*100).__str__()
    print(num6)
    res = s.post(url1, headers=header, data=json.dumps(body7))
    str = res.text
    if len(json.loads(str)['page']['rows']) == 0:
        num7 = '0'
    else:
        num7 = json.loads(str)['page']['resultCount']
    print(num7)
    res = s.post(url1, headers=header, data=json.dumps(body8))
    str = res.text
    if len(json.loads(str)['page']['rows']) == 0:
        num8 = '0'
    else:
        num8 = json.loads(str)['page']['resultCount']
    print('num8------',num8)
    res = s.post(url1, headers=header, data=json.dumps(body9))
    str = res.text
    if len(json.loads(str)['page']['rows']) == 0:
        num9 = '0'
    else:
        num9 = int(json.loads(str)['page']['resultCount'])
    print('num9-------',num9)
    res = s.post(url1, headers=header, data=json.dumps(body11))
    str = res.text
    if len(json.loads(str)['page']['rows']) == 0:
        num11 = '0'
    else:
        num11 = int(json.loads(str)['page']['resultCount'])
        print('num11-----------',num11)
    temp = int(num9) + int(num8) + int(num11)
    monresolve = temp.__str__()
    print(monresolve)
    sql_insert = "update issuenum set daytotal"+"="+num1+"," +"dayresolve"+ "="+dayresolve+ "," + "montotal"+"="+num7+"," +"monresolve"+ "="+monresolve +","+"lowgrademonth"+ "="+num3+ "," +"highgradepermon"+ "="+num4 + "," +"commentpermon"+ "="+num5+"," + "resolvepermon"+ "="+num6 +","+"updatetime" + "=" + "'" + now.strftime('%Y-%m-%d %H:%M:%S')+ "'" +";"
    sql_insert = sql_insert.encode("utf8")
    print(sql_insert)
    cur.execute(sql_insert)  # 执行上述sql命令
    conn.commit()
    conn.close()
